<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>结构体声明</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/struct","wgTitle":"c/language/struct","wgCurRevisionId":68921,"wgArticleId":7972,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/struct","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_struct skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">结构体声明</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../keyword.html" title="c/keyword">关键词</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor">预处理器</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="statements.html" title="c/language/statements">语句</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html" title="c/language/expressions">表达式</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="c/language/initialization">初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="declarations.html" title="c/language/declarations">声明</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="functions.html" title="c/language/functions">函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="history.html" title="c/language/history">C 的历史</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="c/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="c/language/pointer">指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="c/language/array">数组</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="c/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><strong class="selflink"><span class="t-lines"><span>struct</span></span></strong></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="c/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bit_field.html" title="c/language/bit field">位域</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="atomic.html" title="c/language/atomic">原子类型</a> <span class="t-mark-rev t-since-c11">(C11)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="const.html" title="c/language/const"><span class="t-lines"><span>const</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="volatile.html" title="c/language/volatile"><span class="t-lines"><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="restrict.html" title="c/language/restrict"><span class="t-lines"><span>restrict</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Alignas.html" title="c/language/ Alignas"><span class="t-lines"><span>_Alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="c/language/storage duration">存储期与链接</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="extern.html" title="c/language/extern">外部及试探性定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="c/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Static_assert.html" title="c/language/ Static assert"><span class="t-lines"><span>_Static_assert</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="c/language/attributes"> 属性</a> <span class="t-mark-rev t-since-c23">(C23)</span></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>结构体是一种由一序列的成员组成的类型，成员的存储以顺序分配于内存中（与联合体相反，联合体是由一个序列的成员组成的类型，成员存储在内存中重叠）。
</p><p>结构体的<a href="declarations.html" title="c/language/declarations">类型说明符</a>与<a href="union.html" title="c/language/union">联合体（ <code>union</code> ）</a>类型说明符相同，只是所用的关键词有别。
</p>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95">语法</span></h3>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>struct</b></code> <span class="t-spar">属性说明符序列</span><span class="t-mark">(可选)</span> <span class="t-spar">名字</span><span class="t-mark">(可选)</span> <code><b>{ </b></code> <span class="t-spar">结构体声明列表</span> <code><b>} </b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>struct</b></code> <span class="t-spar">属性说明符序列</span><span class="t-mark">(可选)</span> <span class="t-spar">名字</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 结构体定义：引入一个新类型 struct <span class="t-spar">名字</span> 并定义其含义</div>
<div class="t-li1"><span class="t-li">2)</span> 若仅在其自身的行使用，如在 <code>struct</code> <span class="t-spar">名字</span> <code><b>;</b></code> 中，<i>声明</i>但不定义 struct <code>name</code> （见下方前置声明）。在其他语境中，命名先前声明的结构体，并且不允许 <span class="t-spar">属性说明符序列</span> 。</div>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">名字</span>
</td>
<td> -
</td>
<td>  正在定义的结构体名称
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">结构体声明列表</span>
</td>
<td> -
</td>
<td>  任意数量的变量声明、<a href="bit_field.html" title="c/language/bit field">位域</a>声明和<a href="_Static_assert.html" title="c/language/ Static assert">静态断言</a>声明。不允许不完整类型的成员和函数类型的成员（除了下面描述的柔性数组成员）
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">属性说明符序列</span>
</td>
<td> -
</td>
<td>  <span class="t-mark-rev t-since-c23">(C23)</span><a href="attributes.html" title="c/language/attributes">属性</a>的可选列表，应用到结构体类型
</td></tr></table>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p>在结构体对象内，其成员的地址（及位域分配单元的地址）按照成员定义的顺序递增。能转型指向结构体的指针为指向其首成员（或者若首成员为位域，则指向其分配单元）的指针。类似地，能转型指向结构体首成员的指针为指向整个结构体的指针。在任意二个成员间和最后的成员后可能存在无名的填充字节，但首成员前不会有。结构体的大小至少与其成员的大小之和一样大。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<p>若结构体定义了至少一个具名成员，则额外声明其最后成员拥有不完整的数组类型。访问柔性数组成员的元素时（在以柔性数组成员名为 <code><b>.</b></code> 或 <code><b>-&gt;</b></code> 的右侧运算数的表达式中），结构体表现得如同该数组成员拥有为此对象分配的内存中最长的适合大小。若未分配额外存储，则它表现为如同有 1 个元素的数组，除了若访问该元素，或产生指向该元素后一位置指针，则行为未定义。初始化、 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">sizeof</span></span></span> 及赋值运算符忽略柔性数组成员。拥有柔性数组成员的结构体（或拥有可能递归的有柔性数组成员的结构体成员的联合体）不能作为数组元素，或其他结构体的成员出现。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> s <span class="br0">{</span> <span class="kw4">int</span> n<span class="sy4">;</span> <span class="kw4">double</span> d<span class="br0">[</span><span class="br0">]</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// s.d 是柔性数组元素 </span>
 
    <span class="kw1">struct</span> s t1 <span class="sy1">=</span> <span class="br0">{</span> <span class="nu0">0</span> <span class="br0">}</span><span class="sy4">;</span>         <span class="co1">// OK ： d 如同为 double d[1] ，但访问是 UB</span>
    <span class="kw1">struct</span> s t2 <span class="sy1">=</span> <span class="br0">{</span> <span class="nu0">1</span>, <span class="br0">{</span> <span class="nu16">4.2</span> <span class="br0">}</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 错误：初始化忽略柔性数组</span>
 
    <span class="co1">// 若 sizeof (double) == 8</span>
    <span class="kw1">struct</span> s <span class="sy2">*</span>s1 <span class="sy1">=</span> <a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="kw4">sizeof</span> <span class="br0">(</span><span class="kw1">struct</span> s<span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">64</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 如同 d 为 double d[8]</span>
    <span class="kw1">struct</span> s <span class="sy2">*</span>s2 <span class="sy1">=</span> <a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="kw4">sizeof</span> <span class="br0">(</span><span class="kw1">struct</span> s<span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">46</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 如同 d 为 double d[5]</span>
 
    s1 <span class="sy1">=</span> <a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="kw4">sizeof</span> <span class="br0">(</span><span class="kw1">struct</span> s<span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">10</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 现在如同 d 为 double d[1]</span>
    s2 <span class="sy1">=</span> <a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="kw4">sizeof</span> <span class="br0">(</span><span class="kw1">struct</span> s<span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">6</span><span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 同上，但访问是 UB</span>
    <span class="kw4">double</span> <span class="sy2">*</span>dp <span class="sy1">=</span> <span class="sy3">&amp;</span><span class="br0">(</span>s1<span class="sy2">-</span><span class="sy1">&gt;</span>d<span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>    <span class="co1">//  OK</span>
    <span class="sy2">*</span>dp <span class="sy1">=</span> <span class="nu0">42</span><span class="sy4">;</span>                    <span class="co1">//  OK</span>
    dp <span class="sy1">=</span> <span class="sy3">&amp;</span><span class="br0">(</span>s2<span class="sy2">-</span><span class="sy1">&gt;</span>d<span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>            <span class="co1">//  OK</span>
    <span class="sy2">*</span>dp <span class="sy1">=</span> <span class="nu0">42</span><span class="sy4">;</span>                    <span class="co1">//  未定义行为</span>
 
    <span class="sy2">*</span>s1 <span class="sy1">=</span> <span class="sy2">*</span>s2<span class="sy4">;</span> <span class="co1">// 只复制 s.n ，没有任何 s.d 的元素</span>
               <span class="co1">// 除了 sizeof (struct s) 中捕获的元素</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-c99">(C99 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<p>类似联合体，类型为不带 <span class="t-spar">名字</span> 的结构体的无名结构体成员被称作<i>匿名结构体</i>。每个匿名结构体的成员被认为是外围结构体或联合体的成员。若外围结构体或联合体亦为匿名，则递归应用此规则。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> v <span class="br0">{</span>
   <span class="kw1">union</span> <span class="br0">{</span> <span class="co1">// 匿名联合体</span>
      <span class="kw1">struct</span> <span class="br0">{</span> <span class="kw4">int</span> i, j<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 匿名结构体</span>
      <span class="kw1">struct</span> <span class="br0">{</span> <span class="kw4">long</span> k, l<span class="sy4">;</span> <span class="br0">}</span> w<span class="sy4">;</span>
   <span class="br0">}</span><span class="sy4">;</span>
   <span class="kw4">int</span> m<span class="sy4">;</span>
<span class="br0">}</span> v1<span class="sy4">;</span>
 
v1.<span class="me1">i</span> <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>   <span class="co1">// 合法</span>
v1.<span class="me1">k</span> <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span>   <span class="co1">// 非法：内层结构体非匿名</span>
v1.<span class="me1">w</span>.<span class="me1">k</span> <span class="sy1">=</span> <span class="nu0">5</span><span class="sy4">;</span> <span class="co1">// 合法</span></pre></div></div>
<p>类似联合体，若不以任何具名成员定义结构体（包含经由匿名嵌套结构体或联合体获得的成员），则程序行为未定义。
</p>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E5.89.8D.E7.BD.AE.E5.A3.B0.E6.98.8E">前置声明</span></h3>
<p>下列形式的声明
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>struct</b></code> <span class="t-spar">属性说明符序列</span><span class="t-mark">(可选)</span> <span class="t-spar">名字</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>隐藏任何先前在标签命名空间中声明的 <span class="t-spar">名字</span> 的含义，并在当前作用域中声明 <span class="t-spar">名字</span> 为新的结构体名，可以在之后定义该结构体。在定义出现之前，此结构体名拥有<a href="type.html#.E4.B8.8D.E5.AE.8C.E6.95.B4.E7.B1.BB.E5.9E.8B" title="c/language/types">不完整类型</a>。
</p><p>这允许结构体彼此引用：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> y<span class="sy4">;</span>
<span class="kw1">struct</span> x <span class="br0">{</span> <span class="kw1">struct</span> y <span class="sy2">*</span>p<span class="sy4">;</span> <span class="coMULTI">/* ... */</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> y <span class="br0">{</span> <span class="kw1">struct</span> x <span class="sy2">*</span>q<span class="sy4">;</span> <span class="coMULTI">/* ... */</span> <span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>注意，亦可只用在另一声明中使用 struct 标签引入新的结构体名，但若先前声明的拥有同名的结构体存在于标签<a href="name_space.html" title="c/language/name space">命名空间</a>中，则标签会指代该名称
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw1">struct</span> s<span class="sy2">*</span> p <span class="sy1">=</span> <a href="../types/NULL.html"><span class="kw103">NULL</span></a><span class="sy4">;</span> <span class="co1">// 标签命名一个位置结构体，声明它</span>
<span class="kw1">struct</span> s <span class="br0">{</span> <span class="kw4">int</span> a<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// p 所指向的结构体的定义</span>
<span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">struct</span> s<span class="sy4">;</span> <span class="co1">// 新的局部 struct s 的前置声明</span>
              <span class="co1">// 它隐藏全局 struct s 直至此块结束</span>
    <span class="kw1">struct</span> s <span class="sy2">*</span>p<span class="sy4">;</span>  <span class="co1">// 指向局部 struct s 的指针</span>
                  <span class="co1">// 若无上面的前置声明，则它会指向文件作用域的 s</span>
    <span class="kw1">struct</span> s <span class="br0">{</span> <span class="kw4">char</span><span class="sy2">*</span> p<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 局部 struct s 的定义</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.85.B3.E9.94.AE.E8.AF.8D">关键词</span></h3>
<p><a href="../keyword/struct.html" title="c/keyword/struct"><tt>struct</tt></a>
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p>涉及结构体初始化器的规则，见<a href="struct_initialization.html" title="c/language/struct initialization">结构体初始化</a>。
</p><p>因为不允许不完整类型的成员，而且结构体类型在其定义结束前不完整，故结构体不能拥有有其自身类型的成员。允许指向其自身类型的指针成员是允许的，而这通常用于实现链表或树的节点。
</p><p>因为结构体声明不建立<a href="scope.html" title="c/language/scope">作用域</a>，故在 <span class="t-spar">结构体声明列表</span> 中引入的嵌套类型、枚举及枚举项会在定义结构体的外围作用域可见。
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include &lt;stddef.h&gt;</span>
<span class="co2">#include &lt;stdio.h&gt;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">struct</span> car <span class="br0">{</span> <span class="kw4">char</span> <span class="sy2">*</span>make<span class="sy4">;</span> <span class="kw4">char</span> <span class="sy2">*</span>model<span class="sy4">;</span> <span class="kw4">int</span> year<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 声明结构体类型</span>
    <span class="co1">// 声明并初始化之前声明的结构体类型的对象</span>
    <span class="kw1">struct</span> car c <span class="sy1">=</span> <span class="br0">{</span>.<span class="me1">year</span><span class="sy1">=</span><span class="nu0">1923</span>, .<span class="me1">make</span><span class="sy1">=</span><span class="st0">"Nash"</span>, .<span class="me1">model</span><span class="sy1">=</span><span class="st0">"48 Sports Touring Car"</span><span class="br0">}</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"car: %d %s %s<span class="es1">\n</span>"</span>, c.<span class="me1">year</span>, c.<span class="me1">make</span>, c.<span class="me1">model</span><span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// 声明结构体类型、该类型的对象和指向它的指针</span>
    <span class="kw1">struct</span> spaceship <span class="br0">{</span> <span class="kw4">char</span> <span class="sy2">*</span>make<span class="sy4">;</span> <span class="kw4">char</span> <span class="sy2">*</span>model<span class="sy4">;</span> <span class="kw4">char</span> <span class="sy2">*</span>year<span class="sy4">;</span> <span class="br0">}</span>
        ship <span class="sy1">=</span> <span class="br0">{</span><span class="st0">"Incom Corporation"</span>, <span class="st0">"T-65 X-wing starfighter"</span>, <span class="st0">"128 ABY"</span><span class="br0">}</span>,
        <span class="sy2">*</span>pship <span class="sy1">=</span> <span class="sy3">&amp;</span>ship<span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"spaceship: %s %s %s<span class="es1">\n</span>"</span>, ship.<span class="me1">year</span>, ship.<span class="me1">make</span>, ship.<span class="me1">model</span><span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// 地址以声明顺序递增</span>
    <span class="co1">// 可能插入填充字节</span>
    <span class="kw1">struct</span> A <span class="br0">{</span> <span class="kw4">char</span> a<span class="sy4">;</span> <span class="kw4">double</span> b<span class="sy4">;</span> <span class="kw4">char</span> c<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"offset of char a = %zu<span class="es1">\n</span>offset of double b = %zu<span class="es1">\n</span>offset of char c = %zu<span class="es1">\n</span>"</span>
           <span class="st0">"sizeof(struct A) = %zu<span class="es1">\n</span>"</span>, <a href="../types/offsetof.html"><span class="kw105">offsetof</span></a><span class="br0">(</span><span class="kw1">struct</span> A, a<span class="br0">)</span>, <a href="../types/offsetof.html"><span class="kw105">offsetof</span></a><span class="br0">(</span><span class="kw1">struct</span> A, b<span class="br0">)</span>,
           <a href="../types/offsetof.html"><span class="kw105">offsetof</span></a><span class="br0">(</span><span class="kw1">struct</span> A, c<span class="br0">)</span>, <span class="kw4">sizeof</span><span class="br0">(</span><span class="kw1">struct</span> A<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw1">struct</span> B <span class="br0">{</span> <span class="kw4">char</span> a<span class="sy4">;</span> <span class="kw4">char</span> b<span class="sy4">;</span> <span class="kw4">double</span> c<span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"offset of char a = %zu<span class="es1">\n</span>offset of char b = %zu<span class="es1">\n</span>offset of double c = %zu<span class="es1">\n</span>"</span>
           <span class="st0">"sizeof(struct B) = %zu<span class="es1">\n</span>"</span>, <a href="../types/offsetof.html"><span class="kw105">offsetof</span></a><span class="br0">(</span><span class="kw1">struct</span> B, a<span class="br0">)</span>, <a href="../types/offsetof.html"><span class="kw105">offsetof</span></a><span class="br0">(</span><span class="kw1">struct</span> B, b<span class="br0">)</span>,
           <a href="../types/offsetof.html"><span class="kw105">offsetof</span></a><span class="br0">(</span><span class="kw1">struct</span> B, c<span class="br0">)</span>, <span class="kw4">sizeof</span><span class="br0">(</span><span class="kw1">struct</span> B<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// 能转型指向结构体的指针为指向其首成员的指针，反之亦然</span>
    <span class="kw4">char</span><span class="sy2">*</span> pmake <span class="sy1">=</span> <span class="br0">(</span><span class="kw4">char</span><span class="sy2">*</span><span class="br0">)</span>pship<span class="sy4">;</span>
    pship <span class="sy1">=</span> <span class="br0">(</span><span class="kw1">struct</span> spaceship <span class="sy2">*</span><span class="br0">)</span>pmake<span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">car: 1923 Nash 48 Sports Touring Car
spaceship: 128 ABY Incom Corporation T-65 X-wing starfighter
offset of char a = 0
offset of double b = 8
offset of char c = 16
sizeof(struct A) = 24
offset of char a = 0
offset of char b = 1
offset of double c = 8
sizeof(struct B) = 16</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-17">
<ul><li> C17 标准（ISO/IEC 9899:2018）：
</li></ul>
<dl><dd><ul><li> 6.7.2.1 Structure and union specifiers (p: 81-84)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-11">
<ul><li> C11 标准（ISO/IEC 9899:2011）：
</li></ul>
<dl><dd><ul><li> 6.7.2.1 Structure and union specifiers (p: 112-117)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c99">
<ul><li> C99 标准（ISO/IEC 9899:1999）：
</li></ul>
<dl><dd><ul><li> 6.7.2.1 Structure and union specifiers (p: 101-104)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c89">
<ul><li> C89/C90 标准（ISO/IEC 9899:1990）： 
</li></ul>
<dl><dd><ul><li> 3.5.2.1 Structure and union specifiers 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<ul><li> <a href="operator_member_access.html" title="c/language/operator member access">结构体及联合体成员访问</a>
</li><li> <a href="bit_field.html" title="c/language/bit field">位域</a>
</li><li> <a href="struct_initialization.html" title="c/language/struct initialization">结构体初始化</a>
</li></ul>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 2200/1000000
Preprocessor generated node count: 6255/1000000
Post‐expand include size: 51651/2097152 bytes
Template argument size: 17159/2097152 bytes
Highest expansion depth: 16/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:7972-0!*!0!!zh!*!zh!* and timestamp 20211207194649 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/struct&amp;oldid=68921">https://zh.cppreference.com/mwiki/index.php?title=c/language/struct&amp;oldid=68921</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/struct">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年11月27日 (星期五) 20:35。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过11,085次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.586 secs. -->
	</body>
<!-- Cached 20211207194649 -->
</html>